package com.lysh.simple;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.util.StopWatch;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * @author liyishan
 * @date 2023/8/4 15:31
 * @apiNote
 */

public class Main {
    private static final String aesKey = "7CWx7IKEwHoWQ//T4Yh7KQ==";

    private static final String privateKey = "MIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBAI8W4iLzoi8xXrePwYpYJDROOXDNRwYjp3gRWv8NYlWsgmHxX9kL9WnthD9hn3SFY3UXJVIg0cQJPEGgkCMDAMC7sa0dlVEvRWcAxfMSu/0v4nAIrxggyXPagB89ByDZn9VwUyNWUUPVzX6NL3BbHhy1FAFSc7nkSrB9zX7j+v27AgMBAAECgYEAiwPpaZmXrQ/i/KV+m+CsM+D3r17QLOfA0UbTSE4BhkjWVz+BJkaXBHXMcSNwHm1lRiHZkWPvR+zNDNloj4tgU2tLGMuP/Zn3TQUepz0TuhWupv4kql1tBnsoqR4Z6iTGWQrVJjACtVeUzsuKBobYtzw3kv3zGYF3dS9kW/WbJ3ECQQDDtrZZNzpeDRwaXx02BOLAC4CZU7AjCY/dKpwMHbBVLSlo17xUFlGYx0la0G0nYEAVuDk2TYAvK7Zm+pUWoPAjAkEAuypnSGc0DT2m9aY+ceLfImcOtmx63i1+kD6jRNeaVF3m5i2WFglXgoNLFoEuDQeYtauDxL9mJUs1rhp2tOXJiQJAK5XOKDIvcnCS+KCD/dq6sLAfFIZZrIPdoRBzLhOSiHWFSk75JvTNeSuwf2P5WxKJUwzy2NALovWmKmrVtvNiZwJBAIdiA7V2mUsell9H8hFU5swuuZCw5StCwzK5bEYGomZ2Seejf9XOyhLov2624EWigg6e0sCFJfDB90FJeSYetOkCQQCzP2ikjSvH1gGL+10YobQsxAaIgHME/UDPJMGQlcK44ga1LkqXn9g0cGhkXpOpxtVEJzOmhRlhs5dHZtluGQpx";

    private static final String COOPERATOR_ID = "C634713576866582528";
    private static final String GATEWAY_URL = "http://boapi.hvyogo.com";


    public static void main(String[] args)throws Exception {
        bankCarFour();
    }

    public static void bankCarFour()throws Exception{
        Map<String, Object> params = initParams();
        params.put("workerName","李怡珊");
        params.put("workerType",1);
        params.put("idNumber","14243019971008002X");
        params.put("bankCard","6227920658676478");
        params.put("phoneNo", "18101203351");
        String s1 = RSAUtils.sortParam(params);
        String sign = RSAUtils.sign(s1.getBytes(), privateKey);
        params.put("sign", sign);
        String jsonString = JSON.toJSONString(params);
        String s = AESUtils.encrypt2Hex(jsonString, aesKey);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("cooperatorId", COOPERATOR_ID);
        jsonObject.put("businessBody", s);
        String res = HttpUtils.postJson(GATEWAY_URL + "/api/worker/bank/card/four/element/check", jsonObject.toJSONString());
        System.out.println(res);
        System.out.println();
        Map map1 = JSON.parseObject(res, Map.class);
        if (map1.get("data") != null) {
            String data = String.valueOf(map1.get("data"));
            String s2 = AESUtils.decryptByHex(data, aesKey);
            System.out.println(JSON.toJSON(s2));
        }

    }

    public static void agreement()throws Exception{
        Map<String, Object> params = initParams();
        params.put("workerType",1);
        params.put("idCard","14243019971008002X");
        params.put("workerName","李怡珊");
        File file = new File("/Users/liyishan/Desktop/picture/20150919022913sFUmNsFUmN.jpg");
        double size = file.length()/1024.0;
        // 图片对象
        BufferedImage bufferedImage = ImageIO.read(new FileInputStream(file));
        // 宽度
        int width = bufferedImage.getWidth();
        // 高度
        int height = bufferedImage.getHeight();
        // 打印信息
        System.out.printf("图片大小：%.1fkb；图片宽度：%s像素；图片高度：%s像素", size, width, height);
        params.put("idCardFront",FileUtils.encodeBase64File(file));
        params.put("idCardBack",FileUtils.encodeBase64File(file));
        params.put("workerMobile","18101203351");
        String s1 = RSAUtils.sortParam(params);
        String sign = RSAUtils.sign(s1.getBytes(), privateKey);
        params.put("sign", sign);
        String jsonString = JSON.toJSONString(params);
        String s = AESUtils.encrypt2Hex(jsonString, aesKey);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("cooperatorId", COOPERATOR_ID);
        jsonObject.put("businessBody", s);
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        String res = HttpUtils.postJson(GATEWAY_URL + "/api/worker/agreement", jsonObject.toJSONString());
        stopWatch.stop();
        System.out.println(stopWatch.getTotalTimeMillis());
        System.out.println(res);
        Map map1 = JSON.parseObject(res, Map.class);
        if (map1.get("data") != null) {
            String data = String.valueOf(map1.get("data"));
            String s2 = AESUtils.decryptByHex(data, aesKey);
            System.out.println(JSON.toJSON(s2));
        }
    }
    /**
     * 初始化参数
     *
     * @return params
     */
    private static Map<String, Object> initParams() {
        Map<String, Object> params = new HashMap<>(8);
        params.put("cooperatorId", COOPERATOR_ID);
        params.put("timestamp", String.valueOf(System.currentTimeMillis()));
        return params;
    }
}
